package com.dongxin.mongodbproject.storage.model;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;

/**
 * @author 熊开斌
 */
public abstract class AbstractBaseMongoTemplete implements ApplicationContextAware {
    protected MongoTemplate mongoTemplate;

    protected GridFsTemplate gridFsTemplate;

    /**
     * @Description 根据配置文件设置mongoTemplate
     * @param mongoTemplate
     */
    public void setMongoTemplate(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    public void setGridFsTemplate(GridFsTemplate gridFsTemplate) {
        this.gridFsTemplate = gridFsTemplate;
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        MongoTemplate mongoTemplate = applicationContext.getBean("mongoTemplate", MongoTemplate.class);
        setMongoTemplate(mongoTemplate);
        setGridFsTemplate(gridFsTemplate);
    }
}
